#include "resultform.h"
#include "ui_resultform.h"
#include "pupil.h"

ResultForm::ResultForm(QWidget *parent, Pupil** students,
                       int studentsCount, QString title, QStringList subjectsList) :
    QWidget(parent),
    ui(new Ui::resultform)
{
    ui->setupUi(this);

    this->students = students;
    this->studentsCount = studentsCount;

    setWindowTitle(title);
    subjectsList.push_back(tr("Rating"));
    subjectsList.push_back(tr("Place"));
    ui->tableWidget->setColumnCount(subjectsList.count());
    ui->tableWidget->setRowCount(studentsCount);
    ui->tableWidget->setHorizontalHeaderLabels(subjectsList);

    for (int i = 0; i < studentsCount; i++)
    {
        Pupil* pupil = students[i];

        QTableWidgetItem* nameCell = new QTableWidgetItem(QString(pupil->getName()));
        ui->tableWidget->setItem(i, 0, nameCell);

        for (int j = 0; j < ui->tableWidget->columnCount() - 3; j++)
        {
            QTableWidgetItem* ballCell = new QTableWidgetItem();
            ballCell->setText(
                        QString::number(pupil->getBall(j)) +
                        "[" + QString::number(pupil->getSubjectPlace(j)) + "]" +
                        QString::number(pupil->getBonus(j)));
            ui->tableWidget->setItem(i, j + 1, ballCell);
        }
        QTableWidgetItem* ratingCell = new QTableWidgetItem();
        ratingCell->setText(QString::number(pupil->getRatingBall()));
        ratingCell->setBackground(QColor::fromRgb(28,175,102));
        ui->tableWidget->setItem(i, subjectsList.count() - 2, ratingCell);

        QTableWidgetItem* placeCell = new QTableWidgetItem();
        placeCell->setText(QString::number(pupil->getRatingPlace()));
        placeCell->setBackground(QColor::fromRgb(132,159,255));
        ui->tableWidget->setItem(i, subjectsList.count() - 1, placeCell);

    }

    ui->tableWidget->sortByColumn(subjectsList.count() - 1, Qt::AscendingOrder);
}

ResultForm::~ResultForm()
{
    delete ui;

    delete [] students;
}
